/**
                     GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.
 
 */

package com.pic.text;

import java.io.File;

import com.pic.text.Languages.Language;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class PicText extends Activity // implements Runnable
{

	private static final String TAG = PicText.class.getSimpleName();

	Translate trans;
	String translatedString;
	OCR ocr;
	Intent cameraIntent;
	Bitmap bMap = null;
	Language toLanguage = Language.ENGLISH;
	Language fromLanguage = Language.FRENCH; 
	
	final String SD_CARD_TEMP_DIR = Environment
	.getExternalStorageDirectory()
	+ File.separator + "tmpPhoto.jpg";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		final int PICTURE_ACTIVITY = 1; // This is only really needed if you are
		
		super.onCreate(savedInstanceState);
		//LanguageList();
		setContentView(R.layout.main);
		/*
		final Button cameraButton = (Button) findViewById(R.id.camera_button);
		
		cameraButton.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
						Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
				startActivityForResult(cameraIntent, PICTURE_ACTIVITY);
			}
		});*/

	}
	
	public Language getToLanguage() {
		return toLanguage;
	}

	public void setToLanguage(Language language) {
		this.toLanguage = language;
	}

	public Language getFromLanguage() {
		return fromLanguage;
	}

	public void setFromLanguage(Language fromLanguage) {
		this.fromLanguage = fromLanguage;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

		AlertDialog msgDialog = null;
		if (resultCode == RESULT_CANCELED) {
			msgDialog = createAlertDialog("Damn !!", "The snap was not OK :(",
					"OK!  I'LL TRY AGAIN!");

		} else {
		
			File f = new File(SD_CARD_TEMP_DIR);
			if(f.exists())
			{
				String string = null;
				String french = null;
				//msgDialog = createAlertDialog("Yay !!", "File exists baby", "Yipee");
				ocr = new OCR("http://maggie.ocrgrid.org/cgi-bin/weocr/submit_tesseract.cgi", 10000, f);
				string = ocr.run();
				trans = new Translate();
				
		
				if(string == null) {
					msgDialog = createAlertDialog("F&$k", "File exists baby", "Yipee");
				}else {
					try {
						french = trans.translate(string, "en", getToLanguage().getShortName());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					msgDialog = createAlertDialog(string, french, "Yipee");
				}
			}
			
			f.delete();

		}
		msgDialog.show();
	}

	public AlertDialog createAlertDialog(String title, String msg,
			String buttonText) {
		AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
		AlertDialog msgDialog = dialogBuilder.create();
		msgDialog.setTitle(title);
		msgDialog.setMessage(msg);
		msgDialog.setButton(buttonText, new DialogInterface.OnClickListener() {
			// @Override
			public void onClick(DialogInterface dialog, int idx) {
				return; // Nothing to see here...
			}
		});

		return msgDialog;
	}
	
	 protected void LanguageList() 
	    {
	      
	        LayoutInflater inflater = (LayoutInflater) (this.getSystemService(
	            Context.LAYOUT_INFLATER_SERVICE));
	        ScrollView scrollView = (ScrollView) inflater.inflate(R.layout.language_list, null);
	        setContentView(scrollView);
	        
	        LinearLayout layout = (LinearLayout) scrollView.findViewById(R.id.languages);
	        
	        LinearLayout current = null;
	        Language[] languages = Language.values();
	        for (int i = 0; i < languages.length; i++) {
	            if (current != null) {
	                layout.addView(current);
	            }
	            current = new LinearLayout(this);
	            current.setOrientation(LinearLayout.HORIZONTAL);
	            Button button = (Button) inflater.inflate(R.layout.language_entry, current, false);

	            Language language = languages[i];
	            language.configureButton(this, button);
	            button.setOnClickListener(new OnClickListener() {
			// @Override
			public void onClick(View v) {
				// set the language to translate to
				     setToLanguage((Language) v.getTag());
				     Log.v(TAG,"Language " + v.getTag().toString());
				     //dismiss();
				     setContentView(R.layout.output);
			}
		});
	            current.addView(button, button.getLayoutParams());
	        }
	        if (current != null) {
	            layout.addView(current);
	        }
	        setTitle(" ");  // set later, but necessary to put a non-empty string here
	    }



	public void afterTakingPicture() {
		trans = new Translate();

		try {
			translatedString = trans.translate("Hello", "en", "fr");
			Log.d(TAG, "translation result: " + translatedString);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
